package fr.norsys.mapper.console.service;

import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;

import org.apache.commons.beanutils.BeanUtils;

import uk.ltd.getahead.dwr.ExecutionContext;

import fr.norsys.mapper.console.model.Application;
import fr.norsys.mapper.console.model.Attribute;
import fr.norsys.mapper.console.model.AttributeNameComparator;
import fr.norsys.mapper.console.model.ConsoleCst;
import fr.norsys.mapper.console.model.Resource;
import fr.norsys.mapper.console.utils.UIDGenerator;

public class ResourceServiceImpl implements ResourceService {

	/**
	 * Action to get a resource from an application
	 * @param currentResource (String) : Id of the current resource
	 * @param application (Application) : application of the current resource
	 * @return (Resource)
	 */
	public Resource get(String currentResourceId, Application application) {
		
		Resource resource=null;
		Collection resources = application.getResources();
		for(Iterator it=resources.iterator();it.hasNext();){
			
			Resource r = (Resource)it.next();
			if(r.getId().equals(currentResourceId)){
				resource=r;
			}
		}
		
		return resource;
	}

	/**
	 * Action to save (modify or add) the current resource 
	 * for the application in seession.
	 * @param resource (Resource) : the current resource
	 * @param application (Application) : the current application of the resource
	 */
	public void save(Resource resource,Application application){
		if(resource.getId()!=null && resource.getId().length()>0){
			modify(resource,application);
		}else{
			add(resource,application);
		}
	}

	/**
	 * action to add an application in session
	 * 
	 * @param resource :
	 *            bean of the resource
	 * @param application :
	 *            bean of the application
	 */
	private void add(Resource resource, Application application) {
		Resource r = null;
		try {
			r = (Resource) BeanUtils.cloneBean(resource);
		} catch (Exception e) {
		}
		r.setId(UIDGenerator.generateId());
		application.getResources().add(r);
		ExecutionContext.get().getSession().setAttribute(ConsoleCst.CURRENT_RESOURCE,r);
	}

	/**
	 * action to modify an application in session
	 * 
	 * @param resource :
	 *            bean of the resource
	 * @param application :
	 *            bean of the application
	 */
	private void modify(Resource res, Application currentAppli) {
		Resource currentResource = res;
		Resource r= get(currentResource.getId(),currentAppli);
		Collection attributes = r.getAttributes();
		fr.norsys.mapper.console.utils.BeanUtils.copyFilledProperties(r, currentResource);
		currentResource = r;
		currentResource.setAttributes(attributes);
		ExecutionContext.get().getSession().setAttribute(ConsoleCst.CURRENT_RESOURCE,currentResource);
	}

	/**
	 * action to delete an attribute from the attributes list of the resource
	 * @param currentAttributeId : id of the current attribute
	 * @param currentResource : resource bean
	 */
	public void deleteAttr(String currentAttrId, Resource currentResource) {
		Collection tempAttributes = new TreeSet(new AttributeNameComparator());
		for(Iterator attributes=currentResource.getAttributes().iterator(); attributes.hasNext();){
			Attribute attr =(Attribute)attributes.next();
			if(!attr.getId().equals(currentAttrId)){
				tempAttributes.add(attr);
			}
		}
		currentResource.setAttributes(tempAttributes);	
	}

}
